package com.service;

import com.bean.MenuUrl;
import com.dao.MenuUrlDao;
import com.utils.JsonUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.map.HashedMap;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.awt.*;
import java.math.BigInteger;
import java.util.*;
import java.util.List;

/**
 * Created by Ming on 2016/9/26.
 */
@Service
public class MenuUrlService {
    @Autowired
    private MenuUrlDao menuUrlDao;

    public Map<String,String> getMapMsg(Integer level) {
        TreeMap<Long,List<MenuUrl>> fatherandsonMap = new TreeMap<Long,List<MenuUrl>>();
        if (level<3){
            Map<Long,MenuUrl> parentMap =  menuUrlDao.getFatherIdAndOther_2();
            List<MenuUrl> submenuList = menuUrlDao.getSonAll(level);
            for (MenuUrl menuUrl :submenuList) {
                if (fatherandsonMap.containsKey(menuUrl.getParentId())){
                    List<MenuUrl> list = fatherandsonMap.get(menuUrl.getParentId());
                    list.add(menuUrl);
                    fatherandsonMap.put(menuUrl.getParentId(),list);
                }else {
                    List<MenuUrl> list= new ArrayList<MenuUrl>();
                    list.add(menuUrl);
                    fatherandsonMap.put(menuUrl.getParentId(),list);
                }
            }
            Map<String,String> stringMap = new HashedMap<String, String>();
            stringMap.put("parentMap", JsonUtil.obj2Json(parentMap));
            stringMap.put("fatherandsonMap", JsonUtil.obj2Json(fatherandsonMap));
            return stringMap;
        }else{
            Map<Long,MenuUrl> parentMap =  menuUrlDao.getFatherIdAndOther();
            List<MenuUrl> submenuList = menuUrlDao.getSonAll_2();
            for (MenuUrl menuUrl :submenuList) {
                if (fatherandsonMap.containsKey(menuUrl.getParentId())){
                    List<MenuUrl> list = fatherandsonMap.get(menuUrl.getParentId());
                    list.add(menuUrl);
                    fatherandsonMap.put(menuUrl.getParentId(),list);

                }else {
                    List<MenuUrl> list= new ArrayList<MenuUrl>();
                    list.add(menuUrl);
                    fatherandsonMap.put(menuUrl.getParentId(),list);
                }
            }
            Map<String,String> stringMap = new HashedMap<String, String>();
            stringMap.put("parentMap", JsonUtil.obj2Json(parentMap));
            stringMap.put("fatherandsonMap", JsonUtil.obj2Json(fatherandsonMap));
            return stringMap;
        }
    }

    public List<MenuUrl> getAllMenuMsg(){
        List<MenuUrl> allMenuMsg= menuUrlDao.getAllMenuMsg();
        if (CollectionUtils.isNotEmpty(allMenuMsg)){
            return  allMenuMsg;
        }
        return null;
    }
    public void delMenuMsgById(Long id){
        menuUrlDao.delMenuMsgById(id);
    }
    public int updateMenuMsgById(MenuUrl menuUrl){
        int i = menuUrlDao.updateMenuMsgById(menuUrl);
        return i;
    }
    public int insertMenuMsg(MenuUrl menuUrl){
        int i = menuUrlDao.insertMenuMsg(menuUrl);
        return i;
    }
    public List<MenuUrl> getMenuMsgById(Long id){
        List<MenuUrl> menuMsgById= menuUrlDao.getMenuMsgById(id);
        if (CollectionUtils.isNotEmpty(menuMsgById)){
            return  menuMsgById;
        }
        return null;
    }


}
